<template>
  <div class="login">
    <div class="title">
      <p>宁波善德智慧合约管理系统</p>
      <div style="display: flex">
        <div class="buleLine" />
        <div class="yellowLine" style="" />
      </div>
    </div>
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <h3 class="login-form-title">欢迎登录系统</h3>
      <p style="">欢迎使用，请输入登录账号及密码以登录系统</p>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
          <svg-icon
            slot="prefix"
            icon-class="user"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon
            slot="prefix"
            icon-class="password"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon
            slot="prefix"
            icon-class="validCode"
            class="el-input__icon input-icon"
          />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img" />
        </div>
      </el-form-item>
      <el-checkbox
        v-model="loginForm.rememberMe"
        style="margin: 0px 0px 25px 0px"
        >记住密码</el-checkbox
      >
      <el-form-item style="width: 100%">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          class="edit"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>

        <div style="float: right" v-if="register">
          <router-link class="link-type" :to="'/register'"
            >立即注册</router-link
          >
        </div>
      </el-form-item>
      <div
        style="
          position: absolute;
          bottom: 2%;
          left: 0;
          color: #aaa;
          width: 100%;
          text-align: center;
          font-size: 16px;
        "
      >
        {{ configValue }}
      </div>
    </el-form>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import { getConfig } from "@/api/system/config";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        forceLogin: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      loading: false,
      configValue: "",
      // 验证码开关
      captchaEnabled: false,
      // 注册开关
      register: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getCode();
    this.getCookie();
    this.getparameter();
  },
  methods: {
    getparameter() {
      getConfig(100).then((res) => {
        console.log(res);
        this.configValue = res.data.configValue;
      });
    },
    getCode() {
      getCodeImg().then((res) => {
        this.captchaEnabled =
          res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    //强制登录
    forceLogin() {
      this.loginForm.forceLogin = true;
      console.log(this.loginForm);
      this.$modal
        .confirm("该客户端已有用户在线，是否强制登录？")
        .then(() => {
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: "/contract/Contract" }).catch(() => {});
          });
        })
        .catch(() => {
          this.loading = false;
          if (this.captchaEnabled) {
            this.getCode();
          }
        });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$router.push({ path: "/contract/Contract" }).catch(() => {});
            })
            .catch((res) => {
              console.log("123123123");
              console.log(res);
              this.loading = false;
              if (this.captchaEnabled) {
                this.getCode();
              }
              if (res == "Error: 该客户端已有用户在线，是否强制登录？") {
                this.forceLogin();
              } else {
                let label = String(res).replace("Error: ", "");
                this.$message({
                  message: label,
                  type: "warning",
                });
              }
            });
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  height: 100%; /* 设置高度为视口高度 */
  position: relative;
  background-image: url("../assets/images/login-background.0c30ecf4.png");
  background-size: 100% 100%;
  margin: 0;
  padding: 0;
}
@font-face {
  font-family: "loginFont";
  src: url("../assets/font/login-font.ttf") format("truetype");
  font-weight: normal;
  font-style: normal;
}
.title {
  position: absolute;
  top: 15%;
  left: 8%;
  display: flex;
  flex-direction: column;
  width: 40%;
  background-color: rgba(255, 255, 255, 0.4);
  padding: 10px 30px 30px 30px;

  p {
    font-family: "loginFont", sans-serif;
    color: rgb(25, 149, 247);
    height: 45px;
    font-size: 45px;
    margin: 0 20px 20px 0;
  }

  .buleLine {
    width: 560px;
    height: 12px;
    background-color: rgb(12, 126, 216);
    transform: skew(-40deg, 0deg);
  }
  .yellowLine {
    width: 130px;
    height: 12px;
    background-color: rgb(253, 153, 37);
    margin-left: 5px;
    transform: skew(-40deg, 0deg);
  }
}
.login-form {
  position: absolute;
  right: 12.2%;
  bottom: 15.5%;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.84);
  width: 30%;
  height: 62%;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 40px;
    border: 0;
    background: #f8fafb;
    border-color: transparent; /* 取消边框颜色 */
    box-shadow: none; /* 取消阴影效果 */
    input {
      height: 40px;
      border-color: transparent; /* 取消边框颜色 */
      box-shadow: none; /* 取消阴影效果 */
    }
  }
  .el-input__inner:focus {
    border-color: transparent; /* 取消边框颜色 */
    box-shadow: none; /* 取消阴影效果 */
  }
  .el-input__inner:hover {
    border-color: transparent; /* 取消边框颜色 */
    box-shadow: none; /* 取消阴影效果 */
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
  p {
    font-size: 16px;
    font-weight: 400;
    color: #8297a9;
    margin: 20px;
  }
  .login-form-title {
    font-size: 30px;
    font-family: Microsoft YaHei;
    font-weight: 700;
    color: #637688;
    margin: 4% 0 4% 4.5%;
  }
}
.edit {
  width: 100%;
  height: 52px;
  background: linear-gradient(
    90deg,
    rgb(33, 191, 250) 0%,
    rgb(26, 153, 251) 100%
  );
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>
